package com.kaibes.admin.account;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.kaibes.module.account.core.AccountService;
import com.kaibes.module.account.pojo.Account;
import com.kaibes.orm.core.core.Page;
import com.kaibes.web.thymeleaf.ThymeleafConfig;
import com.kaibes.web.util.WebUtils;
import com.kaibes.web.util.model.PageParam;

@Component
public class AccountUtils implements InitializingBean {
    
    @Autowired
    private ThymeleafConfig thymeleafConfig;
    @Autowired
    private AccountService accountService;
    
    public String getUsernameById(Integer userId) {
        return accountService.getUsernameById(userId);
    }
    public String getUsernameByUserId(Integer userId) {
        return accountService.getUsernameByUserId(userId);
    }
    public long count() {
        return accountService.count();
    }
    
    public Page<Account> pageSearch() {
        PageParam p = WebUtils.getPage(25, 1, "");
        if (p.getSearch().isEmpty()) {
            return accountService.pageAsc(p.getPage(), p.getSize(), "username");
        } else {
            return accountService.pageByUsernameLike(p.getPage(), p.getSize(), p.getSearch());
        }
    }
    
    public Account getByUserId(int userId) {
    	return accountService.getByUserId(userId);
    }

	@Override
	public void afterPropertiesSet() throws Exception {
		thymeleafConfig.putStaticVariable("accountUtils", this);
	}
}
